package cn.kz.hrm.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置类，用来声明队列和创建交换机，同时将队列和交换机进行绑定队列
 */
@Configuration //配置类
public class RabbitmqConfig {
    //创建交换机名字
    public static final String COURSE_EXCHANGE_NAME = "course_exchange_name";
    //队列一 邮件推送
    public static final String QUENE_NAME_EMAIL = "quene_name_email";
    //队列二 短信推送
    public static final String QUENE_NAME_SMS = "quene_name_sms";
    //队列三 站内信
    public static final String QUENE_NAME_SYSTEM = "quene_name_system";


    //创建一个交换机
    @Bean(COURSE_EXCHANGE_NAME)
    public Exchange createExchange() {
        return ExchangeBuilder.topicExchange(COURSE_EXCHANGE_NAME).durable(true).build();
    }


    //创建队列一 邮件
    @Bean(QUENE_NAME_EMAIL)
    public Queue createQueueEmail() {
        Queue queueEmail = new Queue(QUENE_NAME_EMAIL);
        return queueEmail;
    }

    //创建队列二 短信
    @Bean(QUENE_NAME_SMS)
    public Queue createQueueSms() {
        Queue queueSms = new Queue(QUENE_NAME_SMS);
        return queueSms;
    }

    //创建队列三 站内信
    @Bean(QUENE_NAME_SYSTEM)
    public Queue createQueueSystem() {
        Queue queueSystem = new Queue(QUENE_NAME_SYSTEM);
        return queueSystem;
    }



    //将队列一绑定到交换机
    @Bean
    public Binding queueEmailBindExchange(@Qualifier(QUENE_NAME_EMAIL) Queue queue, Exchange exchange){
        Binding noargs = BindingBuilder.bind(queue).to(exchange).with("course.email").noargs();
        return noargs;
    }

    //将队列二绑定到交换机
    @Bean
    public Binding queueSmsBindExchange(@Qualifier(QUENE_NAME_SMS) Queue queue, Exchange exchange){
        Binding noargs = BindingBuilder.bind(queue).to(exchange).with("course.sms").noargs();
        return noargs;
    }

    //将队列二绑定到交换机
    @Bean
    public Binding queueSystemBindExchange(@Qualifier(QUENE_NAME_SYSTEM) Queue queue, Exchange exchange){
        Binding noargs = BindingBuilder.bind(queue).to(exchange).with("course.system").noargs();
        return noargs;
    }
}
